package com.teyao.teyao_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teyao.teyao_back.entity.Dict;
import com.teyao.teyao_back.entity.ResponseMessage;

import com.teyao.teyao_back.entity.Result;
import com.teyao.teyao_back.mapper.DictMapper;

import com.teyao.teyao_back.service.DictService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {
    @Override
    public ResponseMessage selectStatus() {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("super_id", 6);
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setData(baseMapper.selectList(queryWrapper));
        return responseMessage;
    }

    @Override
    public ResponseMessage selectByDesc(String desc) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("descs", desc);
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setData(baseMapper.selectList(queryWrapper));
        return responseMessage;
    }

    @Override
    public ResponseMessage selectClass() {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("super_id", 2);
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setData(baseMapper.selectList(queryWrapper));
        return responseMessage;
    }

    @Override
    public ResponseMessage selectType() {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("super_id", 3);
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setData(baseMapper.selectList(queryWrapper));
        return responseMessage;
    }



    //数据字典列表分页(梁宏宇)
    @Autowired
    private DictMapper mapper;

    @Autowired
    private Result result;

    @Override
    public Result getList(Dict dict, Integer pageSize, long current) {
//        Page<Dict> page1=new Page<>(1,5);

        IPage<Dict> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(current);

        //不定查询
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        if (dict.getDescs() != null && !dict.getDescs().isBlank()) {
            queryWrapper.like("descs", dict.getDescs());
        }
        IPage<Dict> dictPage = mapper.selectPage(page, queryWrapper);
        result.setSuccess(true);
        result.setData(dictPage.getRecords());
        result.setTotal(dictPage.getTotal());

        return result;
    }

    //完成删除数据字典列表功能(梁宏宇)
    @Override
    public ResponseMessage deleteList(Integer dictId){
        // 构建删除条件
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_id" , dictId);//根据dictId字段匹配条件
        ResponseMessage responseMessage= new ResponseMessage();
        //执行删除操作
        int deleteCount = mapper.delete(queryWrapper);

        boolean success = deleteCount>0?true:false;
        responseMessage.setSuccess(success);
        return responseMessage;
    }

    //修改列表
    @Override
    public ResponseMessage updateList(Integer dictId , Dict dict){
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_id" , dictId);
        ResponseMessage responseMessage = new ResponseMessage();

        Dict dictToUpdate = new Dict();
        dictToUpdate.setDescs(dict.getDescs());
        dictToUpdate.setSuperId(dict.getSuperId());
        int updateCount = mapper.update( dictToUpdate, queryWrapper);

        boolean success = updateCount>0?true:false;
        responseMessage.setSuccess(success);
        return responseMessage;
    }

    //数据字典添加功能
    @Override
    public ResponseMessage insertList(Dict dict){

        ResponseMessage responseMessage = new ResponseMessage();
        Dict dict1 = new Dict();
        dict1.setDictId(dict.getDictId());
        dict1.setDescs(dict.getDescs());
        dict1.setSuperId(dict.getSuperId());
        int updateCount = mapper.insert(dict1);

        boolean success = updateCount>0?true:false;
        responseMessage.setSuccess(success);
        return responseMessage;
    }
}
